package com.xm.xmapp.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.amap.api.navi.model.NaviLatLng;
import com.xm.xmapp.BasicNaviActivity;
import com.xm.xmapp.MainActivity;
import com.xm.xmapp.MipcaActivityCapture;
import com.xm.xmapp.PileSelectActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.dialog.PileTipDialog;
import com.xm.xmapp.map.ClusterItem;
import com.xm.xmapp.myinterface.MainActivityInterFace;
import com.xm.xmapp.myinterface.ScrollInterFaceAble;
import com.xm.xmapp.response.bean.CustomHeightBean;
import com.xm.xmapp.response.bean.ResponsePileNetworkInfo;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.Tools;


public class PileInfoFragment extends BaseFragment implements OnClickListener ,ScrollInterFaceAble{
    public final static int SCANNIN_GREQUEST_CODE = 2;

    TextView tvAddress;
    TextView tvCost;
    TextView tvFast;
    TextView tvSlow;

    TextView tvOpenTime;
    RelativeLayout relSubscribe;
    View vLine;
    RelativeLayout relUsecar;
    TextView tvStatus;
    LinearLayout llBtn;
    LinearLayout bottom;
    LinearLayout layout;

    private ResponsePileNetworkInfo bean;
    private String str_juli;
    private ClusterItem item;
    private Rect mRect;


    public PileInfoFragment() {
    }

    public void setData(ClusterItem clusterItem, ClusterItem item, ResponsePileNetworkInfo bean) {
        LatLng latLng = MyAppcation.getMyAppcation().getMyLocation();
        if (latLng != null)
            str_juli = StringTools
                    .getJuliString(AMapUtils.calculateLineDistance(latLng, clusterItem.getLatLng()) / 1000) + "";
        else
            str_juli = "0";

        this.bean = bean;
        this.item = item;
    }

    public static PileInfoFragment newInstance(ClusterItem clusterItem, ClusterItem item, ResponsePileNetworkInfo bean) {
        PileInfoFragment newFragment = new PileInfoFragment();
        newFragment.setArguments(new Bundle());
        newFragment.setData(clusterItem, item, bean);
        return newFragment;

    }

    @Override
    public void ViewTreeObserver() {
        super.ViewTreeObserver();
        CustomHeightBean bean = new CustomHeightBean();
        bean.ExitOffset = layout.getHeight();
        bean.MaxOffset = bottom.getHeight() - llBtn.getHeight();
        initRect();
        bean.flag = MainActivityInterFace.INTT_STATUS_NO_ONCLICK;
        MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE, bean);

    }

    @Override
    public int setContentView() {

        return R.layout.fragment_pile_info;
    }

    @Override
    public void initView() {



        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvCost = (TextView) findViewById(R.id.tv_cost);
        bottom = (LinearLayout) findViewById(R.id.bottom);
        tvFast = (TextView) findViewById(R.id.tv_fast);
        tvSlow = (TextView) findViewById(R.id.tv_slow);
        layout = (LinearLayout) findViewById(R.id.layout);
        tvOpenTime = (TextView) findViewById(R.id.tv_open_time);
        relSubscribe = (RelativeLayout) findViewById(R.id.rel_subscribe);
        vLine = (View) findViewById(R.id.v_line);
        relUsecar = (RelativeLayout) findViewById(R.id.rel_usecar);
        llBtn = (LinearLayout) findViewById(R.id.ll_btn);
        tvStatus = (TextView) findViewById(R.id.tv_status);
    }

    @Override
    public void initData() {

        //        TextView tvAddress;
//        TextView tvCost;
//        TextView tvCarFast;
//        TextView tvSlow;
//        TextView tvOpenTime;
//        RelativeLayout relSubscribe;
//        View vLine;
//        RelativeLayout relUsecar;
//        TextView tvStatus;
//        LinearLayout llBtn;


        tvFast.setText("快充 " + bean.fast_charging_pile_num + "个"
                + (bean.showSelectCharging == 1 ? ("  可用" + bean.availableFastChargingNum + "个") : ""));
        tvSlow.setText("慢充 " + bean.slow_charging_pile_num + "个"
                + (bean.showSelectCharging == 1 ? ("  可用" + bean.availableSlowChargingNum + "个") : ""));

        if (bean.parking_fee == null || bean.parking_fee.equals("")) {
            tvCost.setVisibility(View.INVISIBLE);
        } else {
            tvCost.setVisibility(View.VISIBLE);
            tvCost.setText(bean.parking_fee);
        }

        tvOpenTime.setText(bean.open_time);
        tvAddress.setText(bean.address);
        if (bean.showSelectCharging == 1) {
            tvStatus.setVisibility(View.GONE);
            relSubscribe.setVisibility(View.VISIBLE);
            relUsecar.setVisibility(View.VISIBLE);
            vLine.setVisibility(View.VISIBLE);
        } else {
            tvStatus.setVisibility(View.VISIBLE);
            relSubscribe.setVisibility(View.GONE);
            relUsecar.setVisibility(View.GONE);
            tvStatus.setText("暂无可用充电桩");
            vLine.setVisibility(View.GONE);
        }

    }

    @Override
    public void initEvent() {
        tvCost.setOnClickListener(this);
        relUsecar.setOnClickListener(this);
        relSubscribe.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.go:

                if (MyAppcation.getMyAppcation().getMyLocation() != null) {
                    intent = new Intent(MyAppcation.getMyAppcation(), BasicNaviActivity.class);
                    NaviLatLng mEndLatlng = new NaviLatLng(item.getLatLng().latitude, item.getLatLng().longitude);
                    NaviLatLng mStartLatlng = new NaviLatLng(MyAppcation.getMyAppcation().getMyLocation().latitude, MyAppcation.getMyAppcation().getMyLocation().longitude);
                    intent.putExtra("mStartLatlng", mStartLatlng);
                    intent.putExtra("mEndLatlng", mEndLatlng);
                    startActivity(intent);
                } else {
                    Tools.showMessage("导航失败，未获取到您的位置信息!");
                }
                break;

            case R.id.tv_cost:

                new PileTipDialog(getActivity(), bean.parking_fee).show();

                break;
            case R.id.rel_usecar:

                intent = new Intent(MyAppcation.getMyAppcation(), MipcaActivityCapture.class);
                intent.putExtra("flag", 1);
                getActivity().startActivityForResult(intent, SCANNIN_GREQUEST_CODE);

                break;
            case R.id.rel_subscribe:
                intent = new Intent(MyAppcation.getMyAppcation(), PileSelectActivity.class);
                intent.putExtra("id", item.getNetworkId());
                startActivity(intent);

                break;

            default:
                break;
        }

    }


    @Override
    public void setOffext(int currentProgress) {
        if(llBtn!=null && mRect!=null) {
            llBtn.layout(mRect.left, -currentProgress + mRect.top, mRect.right, mRect.bottom - currentProgress);
        }
    }

    @Override
    public void initRect() {
        if(mRect==null) {
            int left = llBtn.getLeft();
            int top = llBtn.getTop();
            int right = llBtn.getRight();
            int bottom = llBtn.getBottom();
            mRect = new Rect(left, top, right, bottom);
        }
    }
}
